using TUnit.Core.SourceGenerator.Generators;
using TUnit.Core.SourceGenerator.Tests.Options;

namespace TUnit.Core.SourceGenerator.Tests;

internal class UnifiedReflectionFreeTests : TestsBase<TestMetadataGenerator>
{
    [Test]
    public async Task Test_StronglyTypedDelegates_Generation()
    {
        var source = """
        using TUnit.Core;

        namespace TUnit.TestProject;

        public class TypedDelegateTest
        {
            [Test]
            public void TestWithDelegate()
            {
                // Test that uses strongly typed delegates
            }
        }
        """;

        await RunTestWithInlineSource(source);
    }

    [Test]
    public async Task Test_ModuleInitializer_Generation()
    {
        var source = """
        using TUnit.Core;
        using System.Runtime.CompilerServices;

        namespace TUnit.TestProject;

        public class ModuleInitializerTest
        {
            [Test]
            public void TestWithModuleInit()
            {
                // Test that verifies module initializer works
            }
        }
        """;

        await RunTestWithInlineSource(source);
    }

    [Test]
    public async Task Test_AotSafeDataSourceFactories()
    {
        var source = """
        using TUnit.Core;

        namespace TUnit.TestProject;

        public class AotDataSourceTest
        {
            [Test]
            [Arguments(1, 2, 3)]
            [Arguments(4, 5, 6)]
            public void TestWithDataSource(int a, int b, int c)
            {
                // Test with AOT-safe data sources
            }
        }
        """;

        await RunTestWithInlineSource(source);
    }

    [Test]
    public async Task Test_ConfigurationSupport()
    {
        var source = """
        using TUnit.Core;

        namespace TUnit.TestProject;

        public class ConfigurationTest
        {
            [Test]
            public void TestWithConfiguration()
            {
                // Test that uses configuration
            }
        }
        """;

        await RunTestWithInlineSource(source);
    }

    private async Task RunTestWithInlineSource(string source)
    {
        var tempFile = Path.GetTempFileName() + ".cs";
        await File.WriteAllTextAsync(tempFile, source);

        try
        {
            await RunTest(tempFile, new RunTestOptions(), async generatedFiles =>
            {
                // Basic assertion to verify the generator produces output
                await Assert.That(generatedFiles.Length).IsGreaterThan(0);

                // Verify at least one file contains generated content
                var hasGeneratedContent = generatedFiles.Any(f => f.Contains("// <auto-generated/>"));
                await Assert.That(hasGeneratedContent).IsTrue();
            });
        }
        finally
        {
            if (File.Exists(tempFile))
            {
                File.Delete(tempFile);
            }
        }
    }
}
